package com.mmbang.util;

/**
 * <p>Title: 网站</p>
 *
 * <p>Description: 文件属性读写</p>
 *
 * <p>Copyright: Copyright (c) 2013</p>
 *
 * <p>Company: 丫丫</p>
 *
 * @author 杨洁
 * @version 1.0
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;



/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: 上海东方网诚</p>
 *
 * @author 杨洁
 * @version 1.0
 */
public class FileRes {
	public FileRes() {
	}
	
	private static final Properties props = new Properties();

	/* 配置信息写在文件CommonRes.properties中 */
	private static final String filename = "CommonRes.properties";
	//网站配置文件
	private static final String siteFileName = "SiteRes.properties";
	//开始抓取的地址信息(处理抓取到一半突然停止,然后可以从最后的一个地址继续抓取)
	private static final String start_url = "start_url.txt";
	
	private static final String hotSearchword = "hotsearchword.dic";
	private static final String lastActionTime = "lastActionTime.time";
	
	
	/* 读CommonRes配置文件 */
	public static String getPropValue(String propName) {
		String propValue = "";
		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(
				filename);
		try {
			props.load(is);
			propValue = props.getProperty(propName);
			is.close();
		} catch (Exception e) {
			System.err.println("Can not read the Properties file " + filename);
			return null;
		}
		return propValue;
	}
	
	public void writeActionsTime(String value) {
		
	}
	
	
	
	/* 读SiteRes配置文件 */
	public static String getSiteUrl(String siteId) {
		String propValue = "";
		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(
				siteFileName);
		try {
			props.load(is);
			propValue = props.getProperty(siteId);
			is.close();
		} catch (Exception e) {
			System.err.println("Can not read the Properties file "
					+ siteFileName);
			return null;
		}
		return propValue;
	}
	
	//读取listurl.txt得到底级url的数据
	public static Vector getAllList(String siteId) {
		Vector vec = new Vector();
		String filePath = getPropValue("filePath");
		filePath += getSiteUrl(siteId);
		filePath += File.separator + "listurl" + File.separator +"listurl.txt";
		//System.out.println("hahahafilePath:" + filePath);  
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(filePath))); //建立BufferedReader对象，并实例化为br
			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				// System.out.println("Line:" + Line);           	
				vec.add(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
			}
			br.close(); //关闭BufferedReader对象

		} catch (Exception e) {
			System.err.println("Can not read the  file " + filePath);
			System.out.println(e.getMessage());
		}
		return vec;
	}

	
	//blog读取listurl.txt得到底级url的数据
	public static Vector getAllList(String siteId,String blogAuthorName) {
		Vector vec = new Vector();
		String filePath = getPropValue("filePath");
		filePath += getSiteUrl(siteId);
		filePath += File.separator + blogAuthorName + File.separator + "listurl" + File.separator +"listurl.txt";
		//System.out.println("hahahafilePath:" + filePath);  
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(filePath))); //建立BufferedReader对象，并实例化为br
			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				// System.out.println("Line:" + Line);           	
				vec.add(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
			}
			br.close(); //关闭BufferedReader对象

		} catch (Exception e) {
			System.err.println("Can not read the  file " + filePath);
			System.out.println(e.getMessage());
		}
		return vec;
	}
	
	//blog读取bloglist.txt得到各个作者博客的地址
	public static Vector getAllBlogList(String siteId) {
		Vector vec = new Vector();
		String filePath = getPropValue("filePath");
		filePath += getSiteUrl(siteId);
		filePath +=  File.separator +"bloglist.txt";
		//System.out.println("hahahafilePath:" + filePath);  
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(filePath))); //建立BufferedReader对象，并实例化为br
			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				// System.out.println("Line:" + Line);           	
				vec.add(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
			}
			br.close(); //关闭BufferedReader对象

		} catch (Exception e) {
			System.err.println("Can not read the  file " + filePath);
			System.out.println(e.getMessage());
		}
		return vec;
	}	
	
	



	
	

	

	
	
	
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	
	public StringBuffer getXml_thejamy(String type,String webUrl) {
		StringBuffer strBuf = new StringBuffer();
		
		String fileName = "";
		if(type.equals("head")){
			fileName = "head.xml";
		}else if(type.equals("foot")) {
			fileName = "foot.xml";
		}else if(type.equals("content")) {
			fileName = webUrl + File.separator + "content.xml";
		}else{
			fileName = webUrl + File.separator  + type + ".xml";
		}
		
		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(fileName);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br	


			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				//System.out.println("Line:" + Line);           	
				strBuf.append(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
				if(Line != null) {
					strBuf.append("\n");
				}
			}
			br.close();
			is.close();
		} catch (Exception e) {
			System.out.println("Can not read the Properties file " + fileName);
			return null;
		}
		return strBuf;	
	}

	
	
	//String type ("head","foot","content")
	public StringBuffer getXml(String type) {
		StringBuffer strBuf = new StringBuffer();
		
		String fileName = "";
		if(type.equals("head")){
			fileName = "head.xml";
		}else if(type.equals("foot")) {
			fileName = "foot.xml";
		}else if(type.equals("content")) {
			fileName = "www.ptac365.com\\content.xml";
		}
		
		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(fileName);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br	


			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				//System.out.println("Line:" + Line);           	
				strBuf.append(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
				if(Line != null) {
					strBuf.append("\n");
				}
			}
			br.close();
			is.close();
		} catch (Exception e) {
			System.out.println("Can not read the Properties file " + fileName);
			return null;
		}
		return strBuf;	
	}
 
	//根据分类id来找模板
	public StringBuffer getXml(String type,String categoryId) {
		StringBuffer strBuf = new StringBuffer();
		
		String fileName = "";
		if(type.equals("head")){
			fileName = "head.xml";
		}else if(type.equals("foot")) {
			fileName = "foot.xml";
		}else if(type.equals("content")) {
			fileName = categoryId + File.separator + "content.xml";
		}else if(type.equals("content2")) {
			fileName = categoryId+ File.separator + "content2.xml";
		}else if(type.equals("content3")) {
			fileName = categoryId+ File.separator + "content3.xml";
		}
		
		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(fileName);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br	


			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				//System.out.println("Line:" + Line);           	
				strBuf.append(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
				if(Line != null) {
					strBuf.append("\n");
				}
			}
			br.close();
			is.close();
		} catch (Exception e) {
			System.err.println("Can not read the Properties file ");
			return null;
		}
		return strBuf;	
	} 

    

	
	//写xml文件的路径
	public String getXmlFilePath() {
		StringBuffer strBuf = new StringBuffer();
		
		String propValue = "";
		Properties props = new Properties();
		String filename = "CommonRes.properties";

		InputStream is = FileRes.class.getClassLoader().getResourceAsStream(filename);
		try {
			props.load(is);
			propValue = props.getProperty("fielPath");
			is.close();
		} catch (Exception e) {
			System.err.println("Can not read the Properties file " + filename);
			return null;
		}
		return propValue;
	}
	
	
//	得到对应网站的文件列表
	public static Vector getItemurlFileList(String webSite) {
		Vector vec = new Vector();
		String filePath = getPropValue("filePath");
		filePath += webSite;
		File file = new File(filePath);
		String[] filelist = file.list();
System.out.println(filelist.length);		
		for (int i = 0; i < filelist.length; i++) {

			File readfile = new File(filePath + File.separator + filelist[i]);
			String fileName = readfile.getName();
System.out.println("fileName:" + fileName);	

			vec.add(fileName);
			/*
			if (fileName.indexOf("itemurl_") >= 0 && fileName.indexOf(".") >= 0) {
				String fileNum = fileName.substring("itemurl_".length(),
						fileName.indexOf(".txt"));
				
System.out.println(fileNum);				
				if (isNumeric(fileNum)) {
					//System.out.println("fileName:" + fileName);
					vec.add(fileName);
				}

			}
			*/
		}
		//
		//vec = getListSortByFileNum(vec);
		return vec;
	}
	
	
//	得到热门搜索词
	public Vector getHotSearchword() {
		Vector vec = new Vector();
		StringBuffer strBuf = new StringBuffer();
		
		try {
			InputStream is = FileRes.class.getClassLoader().getResourceAsStream(hotSearchword);
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8")); //建立BufferedReader对象，并实例化为br

			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
//System.out.println("Line:" + Line);           	
				vec.add(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
			}
			br.close();
			is.close();
		} catch (Exception e) {
			System.out.println("Can not read the file " + hotSearchword);
			System.out.println("error:" + e.getMessage());
			return null;
		}
		return vec;
	}
	
	
//	得到action最近一次索引到的时间
	public Map getLastActionTime() {
		String time = "";
		Map<String, String> map = new HashMap<String, String>();

		try {
			//InputStream is = FileRes.class.getClassLoader().getResourceAsStream(lastActionTime);
			File f = new File(this.getPropValue("actionsFile")+File.separator+lastActionTime);
			InputStream is = new FileInputStream(f);

			BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br

			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
//System.out.println("Line:" + Line);
				time = Line;
				Line = br.readLine(); //从文件中继续读取一行数据
				
			}
			//begin:(1351534709)end:(1351544709)
			String beginTime = time.substring(time.indexOf("begin:(")+7,time.indexOf(")end:"));
			String endTime = time.substring(time.indexOf("end:(")+5,time.lastIndexOf(")"));
			map.put("begintime", beginTime);
			map.put("endtime", endTime);
			br.close();
			is.close();
			f =null;
		} catch (Exception e) {
			System.out.println("Can not read the file " + lastActionTime);
			System.out.println("error:" + e.getMessage());
			return null;
		}
		return map;
	}	
	
	
	//得到文件内容
	public static StringBuffer getFileContent(String fileName,String webSite) {
		StringBuffer strBuf = new StringBuffer();
		
		String filePath = getPropValue("filePath") +  webSite +  File.separator;
System.out.println(filePath + fileName);
		try {
			//BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br
			FileInputStream is = new FileInputStream(filePath + fileName);
			BufferedReader br = new BufferedReader(new InputStreamReader(is)); //建立BufferedReader对象，并实例化为br

			String Line = br.readLine(); //从文件读取一行字符串
			//判断读取到的字符串是否不为空
			while (Line != null) {
				//System.out.println("Line:" + Line);           	
				strBuf.append(Line);
				Line = br.readLine(); //从文件中继续读取一行数据
				if(Line != null) {
					strBuf.append("\n");
				}
			}
			br.close();
			is.close();
		} catch (Exception e) {
			System.out.println("Can not read the Properties file " + fileName);
			System.out.println("error:" + e.getMessage());
			return null;
		}
		return strBuf;
		
		
		
	
		
		
	}	
	
    
	public static void main(String[] args) {

		FileRes fileRes = new FileRes();
//		System.out.println(fileRes.getPropValue("filePath"));
//		fileRes.getItemurlFileList("www.mysteel.com");
//		System.out.println(fileRes.getXml("head").toString());
//		System.out.println(fileRes.getXml("content").toString());
//		System.out.println(fileRes.getXml("foot").toString());
		
		
//		fileRes.getColumnMapping("www.joyo.com.txt");
		
		
//		Vector vec = fileRes.getHotSearchword();
//		for(int i=0;i<vec.size();i++) {
//			System.out.println(vec.get(i).toString());
//		}
		
		System.out.println(fileRes.getLastActionTime());
		System.out.println(fileRes.getLastActionTime().get("begintime"));
		System.out.println(fileRes.getLastActionTime().get("endtime"));
		

	}
}
